package com.dqhl.community.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.dqhl.community.R;
import com.dqhl.community.model.ComplainRecord;
import com.dqhl.community.util.Config;
import com.dqhl.community.util.TimeUtils;
import com.dqhl.community.util.ViewHolder;

import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by cheng on 2016/9/10.
 * 投诉记录列表adapter
 */
public class ComplainRecordListAdapter extends BaseAdapter {

    private Context context;
    private List<ComplainRecord> complainRecordList;
    private LayoutInflater inflater;

    public ComplainRecordListAdapter(Context context, List<ComplainRecord> complainRecordList) {
        this.context = context;
        this.complainRecordList = complainRecordList;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return complainRecordList.size();
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        if (view == null) {
            view = inflater.inflate(R.layout.lv_item_complain_record, viewGroup, false);
        }

        TextView tv_address = ViewHolder.get(view, R.id.tv_address);
        TextView tv_date = ViewHolder.get(view, R.id.tv_date);
        ImageView iv_pic_1 = ViewHolder.get(view, R.id.iv_pic_1);
        ImageView iv_pic_2 = ViewHolder.get(view, R.id.iv_pic_2);
        ImageView iv_pic_3 = ViewHolder.get(view, R.id.iv_pic_3);
        ImageView iv_pic_4 = ViewHolder.get(view, R.id.iv_pic_4);
        TextView tv_owner_name = ViewHolder.get(view, R.id.tv_owner_name);
        TextView tv_type = ViewHolder.get(view, R.id.tv_type);
        TextView tv_telephone = ViewHolder.get(view, R.id.tv_telephone);
        TextView tv_content = ViewHolder.get(view, R.id.tv_content);

        ComplainRecord complainRecord = complainRecordList.get(i);

        String strAddress = complainRecord.getCommunity_id() + complainRecord.getBuilding_id()
                + complainRecord.getUnit_id() + complainRecord.getResidence_id();
        tv_address.setText(strAddress);
        String date = TimeUtils.timeStamp2Date(complainRecord.getAdd_time(), "MM月dd日 HH:mm");
        tv_date.setText(date);
        String pathPic1 = complainRecord.getPic_a();
        String pathPic2 = complainRecord.getPic_b();
        String pathPic3 = complainRecord.getPic_c();
        String pathPic4 = complainRecord.getPic_d();
        List<String> pathPicList = new ArrayList<>();
        pathPicList.add(pathPic1);
        pathPicList.add(pathPic2);
        pathPicList.add(pathPic3);
        pathPicList.add(pathPic4);

        List<String> nullList = new ArrayList<>();
        nullList.add("");
        pathPicList.removeAll(nullList);

        switch (pathPicList.size()) {
            case 0:
                iv_pic_1.setVisibility(View.GONE);
                iv_pic_2.setVisibility(View.GONE);
                iv_pic_3.setVisibility(View.GONE);
                iv_pic_4.setVisibility(View.GONE);
                break;
            case 1:
                x.image().bind(iv_pic_1, Config.Api.base_img_url + pathPicList.get(0));
                break;
            case 2:
                x.image().bind(iv_pic_1, Config.Api.base_img_url + pathPicList.get(0));
                x.image().bind(iv_pic_2, Config.Api.base_img_url + pathPicList.get(1));
                break;
            case 3:
                x.image().bind(iv_pic_1, Config.Api.base_img_url + pathPicList.get(0));
                x.image().bind(iv_pic_2, Config.Api.base_img_url + pathPicList.get(1));
                x.image().bind(iv_pic_3, Config.Api.base_img_url + pathPicList.get(2));
                break;
            case 4:
                x.image().bind(iv_pic_1, Config.Api.base_img_url + pathPicList.get(0));
                x.image().bind(iv_pic_2, Config.Api.base_img_url + pathPicList.get(1));
                x.image().bind(iv_pic_3, Config.Api.base_img_url + pathPicList.get(2));
                x.image().bind(iv_pic_4, Config.Api.base_img_url + pathPicList.get(3));
                break;
        }

        tv_owner_name.setText(complainRecord.getName());
        tv_type.setText(complainRecord.getType());
        tv_telephone.setText(complainRecord.getTelephone());
        tv_content.setText(complainRecord.getContent());

        return view;
    }
}
